package com.itheima.controller;

import com.itheima.common.R;
import com.itheima.entity.dto.addressbook.AddressBookUpDto;
import com.itheima.entity.dto.addressbook.AddressBookaddDto;
import com.itheima.entity.pojo.AddressBook;
import com.itheima.entity.vo.addressbook.*;
import com.itheima.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author huangzelnag
 * @create 2022-05-20
 */
@RestController
@RequestMapping("addressBook")
public class AddressBookController {
    @Autowired
    AddressBookService addressBookService;


    /**
     * 添加收货地址
     * @param addressBookaddDto
     * @return
     */
    @PostMapping()
    public R addAddressBook(@RequestBody  AddressBookaddDto addressBookaddDto){

        AddressBookaddVo addressBookaddVo =addressBookService.addressBookService(addressBookaddDto);

    return R.success(addressBookaddVo);
    }


    /**
     * 修改收货地址
     * @param addressBookUpDto
     * @return
     */
    @PutMapping()
    public R upAddressBook(@RequestBody AddressBookUpDto addressBookUpDto){

     Boolean flag =   addressBookService.upAddressBook(addressBookUpDto);

        return R.success("");
    }

    /**
     * 批量删除地址
     * @param ids
     * @return
     */
    @DeleteMapping()
    public R delAddressBook(@RequestParam String ids){
        String[] idStr = ids.split(",");

      Boolean flag =  addressBookService.delAddressBook(idStr);


        return R.success("");
    }


    /**
     * 获取默认收货地址
     * @return
     */
    @GetMapping("/default")
    public R getAddressBookDefault(){
        AddressBookDefaultVo addressBookDefaultVo = addressBookService.getAddressBookDefault();

        return R.success(addressBookDefaultVo);
    }


    /**
     * 设置默认收货地址列表
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R saveAddressBookDefault(@RequestBody AddressBook addressBook){

        AddressBookDefaultSave addressBookDefaultVo = addressBookService.saveAddressBookDefault(addressBook);

        return R.success(addressBookDefaultVo);
    }

    /**
     * 获取收货地址列表
     * @return
     */
    @GetMapping("list")
    public R getAddressBookList(){

        List<AddressBookListVo> addressBookListVo = addressBookService.getAddressBookList();
        return R.success(addressBookListVo);
    }



    @GetMapping("/{id}")
    public R getAddressBookById(@PathVariable String id){

       AddressBookByIdVo addressBookByIdVo= addressBookService.getAddressBookById(id);

        return R.success(addressBookByIdVo);
    }


}
